package cn.kgc.controller;

import cn.kgc.pojo.DeptInf;
import cn.kgc.pojo.JobInf;
import cn.kgc.service.DeptService;
import cn.kgc.service.JobService;
import cn.kgc.utils.PageInfo;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("job")
public class JobController {

    private  static Logger logger = Logger.getLogger(JobController.class);

    @Resource
    JobService jobService;


    @RequestMapping("/limit")
    public String limit(@RequestParam(value = "currentPageNo",required = false) Integer currentPageNo,
                        @RequestParam(value = "pageSize",required = false) Integer pageSize,
                        @RequestParam(value = "NAME",required = false) String NAME,
                        Model model){

        PageInfo pageInfo = new PageInfo();
        if (currentPageNo !=null){
            pageInfo.setCurrentPageNo(currentPageNo);
        }
        if (pageSize!=null){
            pageInfo.setPageSize(pageSize);
        }
        int totalCount = jobService.getCount(NAME);
        pageInfo.setTotalConut(totalCount);
        List<JobInf> list =jobService.getJobByPage(pageInfo.getCurrentPageNo(),pageInfo.getPageSize(),NAME);
        pageInfo.setData(list);
        model.addAttribute("job",pageInfo);
        model.addAttribute("NAME",NAME);

        return "jobLimit";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(@RequestParam("id")Integer id,Model model){
        JobInf jobInf = jobService.getJobById(id);
        model.addAttribute("job",jobInf);
        return "jobUpdate";
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(JobInf jobInf){
        boolean flag = jobService.updateJob(jobInf);
        if (flag){
            return "redirect:/job/limit";
        }else {
            return "jobUpdate";
        }
    }
    @RequestMapping("/toDel")
    public String toDel(@RequestParam("id")Integer id){
        boolean flag = jobService.delId(id);
        if (flag){
            return "redirect:/job/limit";
        }else {
            return "jobLimit";
        }

    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "jobAdd";
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(JobInf jobInf){
        boolean flag = jobService.addJob(jobInf);
        if (flag){
            return "redirect:/job/toAdd";
        }else {
            return "index";
        }
    }

    @RequestMapping("/jobIsExist/{NAME}")
    @ResponseBody
    public JSONObject jobIsExist(@PathVariable String NAME) {

        JobInf jobInf = jobService.getJobNAME(NAME);
        JSONObject jsonObject = new JSONObject();
        if (jobInf == null) {
            jsonObject.put("code", "10000");
            jsonObject.put("msg", "可以使用");
        } else {
            jsonObject.put("code", "10001");
            jsonObject.put("msg", "不可以使用");
        }
        return jsonObject;
    }
}
